package com.prashantdctm.paidemails;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.logging.Logger;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class OnePercent {
	final static boolean PROXY_ENABLED = false;
	private static WebClient webClient = null;
	private static String oldAccountBalance=null;
	private static String newAccountBalance=null;
	static Logger log = Logger.getLogger("OnePercent");
	
	public static void main (String[] args) throws Exception {
		try {
			start();
			
			
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public static void start() throws Exception {
		final boolean proxyEnabled = false;
		WebClient webClient = null;
		
		if(proxyEnabled) {
			webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6,"10.113.26.80",3128);
			//set proxy username and password 
	        final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider();
	        credentialsProvider.addProxyCredentials("prashantkumar", "Qandi!@90");
	    } else {
			webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6);
		}
		
        webClient.setRefreshHandler(new RefreshHandler() {             
        	public void handleRefresh(Page page, URL url, int arg) throws IOException {                 
        		      
        		}
        	}
        ); 
        
        //webClient.setProxyConfig(pc);
        webClient.setPopupBlockerEnabled(true);
        webClient.setTimeout(50000); // timout in ms
        webClient.setRedirectEnabled(true); // follow redirects
        webClient.setThrowExceptionOnFailingStatusCode(false);
        webClient.setJavaScriptEnabled(true);
        webClient.setThrowExceptionOnScriptError(false);
        WebWindow mainWindow = webClient.getCurrentWindow();
        
	    // Get the first page (login), fill form and submit 
	    HtmlPage page = webClient.getPage("http://tokes.1percent.com/main.phtml?page=rewards_home&");
	    HtmlForm form = page.getFormByName("form");
	    
	    form.getInputByName("login_user_id").setValueAttribute("pkjsp");         
	    form.getInputByName("login_password").setValueAttribute("rintu123"); 
	    page = (HtmlPage) form.getInputByValue("Submit").click(); 
	    //dumpPage(page);
	    webClient.waitForBackgroundJavaScript(10000);
        webClient.waitForBackgroundJavaScriptStartingBefore(10000);
        
	    HtmlAnchor anchor = page.getAnchorByHref("/main.phtml?page=site_activities&");
		page = anchor.click();
		//dumpPage(page);
		
		List <HtmlElement> elements = (List<HtmlElement>) page.getByXPath("//img[@alt = 'Total Tokes']");
		for(HtmlElement element : elements) {
			log.info("Current Balance:");
			log.info("Total Tokes - "+element.getParentNode().getParentNode().getNextSibling().getFirstChild().getFirstChild().getTextContent())		}
		
		List<HtmlAnchor> anchors = page.getAnchors();
		for(HtmlAnchor anchor1 : anchors) {
			if(anchor1.getHrefAttribute().contains("action=web_links&")) {
				log.info("Clicking toke task "+anchor1.getFirstChild().getTextContent());
				anchor1.click();
			}
		}
		
		elements = (List<HtmlElement>) page.getByXPath("//img[@alt = 'Total Tokes']");
		for(HtmlElement element : elements) {
			log.info("Current Balance:");
			log.info("Total Tokes - "+element.getParentNode().getParentNode().getNextSibling().getFirstChild().getFirstChild().getTextContent());
		}
		
        webClient.closeAllWindows();
	}
	
	public void dumpPage(HtmlPage page) {
		System.out.println("############# \n"+page.getUrl()+"\n"+page.asXml());
	}
}

//http://www.inboxdollars.com pk.paidemails@gmail.com
//http://www.sendearnings.com pk.paidemails@gmail.com
//http://www.htmail.com pk.paidemails@gmail.com
//https://www.sendmoreinfo.com	3171401 pk.paidemails@gmail.com

//https://www.gomezpeerzone.com pk_jsp	pk_995
//http://tokes.1percent.com pkjsp	rintu123 (click on daily toke tasks)
//http://e-mailpaysu.com pk_jsp pk_995
//http://www.snapdollars.com pkjsp pk_995
//http://www.donkeymails.com pk_jsp	pk_995












